package ltd.followkba.page;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.base.BasePage;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.view.KcBtnText;
import ltd.advskin.view.KcHeaderView;
import ltd.followkba.MAPP;
import ltd.followkba.R;
import ltd.followkba.W;
import ltd.followkba.task.LogTyp;
import ltd.followkba.task.RunUtils;
import main.MAPI;

import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;

public class EditTextPage extends BasePage {
    private TextView tvTipsComment;
    private KcBtnText kbtNewComment;
    private KcBtnText kbtOldComment;
    private EditText etInputComment;


    private int gWorksType;
    private LogTyp gLogTyp;
    private String[] gsType;


    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;

    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_text_edit);
        if (mBaseInput != null) {                   // 可能仅仅需要刷新，未必是重新开始的
            gLogTyp = mBaseInput.getInput(0);
            mBaseInput = null;                  // 输入参数的使命已经完成，记得及时销毁之
        }
        mKcHeaderView = findViewById(R.id.khvComment);

        gsType = MSKIN.getStringArray(R.array.edit_text_type);
        String text = String.format("编辑%s", gsType[gLogTyp.ordinal()]);
        mKcHeaderView.setTitle(text, new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;
                }
            }
        });

        tvTipsComment = findViewById(R.id.tvTipsComment);
        kbtNewComment = findViewById(R.id.kbtNewComment);
        kbtOldComment = findViewById(R.id.kbtOldComment);
        if (gLogTyp != LogTyp.NORMAL_COMMENT){
            kbtNewComment.setVisibility(View.GONE);
            kbtOldComment.setVisibility(View.GONE);
        }else {
            kbtNewComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {                                               // 新作品文字
                   MAPP.startCommentTextPage(LogTyp.NEW_COMMENT);
                }
            });
            kbtOldComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {                                               // 旧作品文字
                    MAPP.startCommentTextPage(LogTyp.OLD_COMMENT);
                }
            });
        }
        findViewById(R.id.kbtOtherComment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                               // 其他账号

            }
        });
        findViewById(R.id.kbtCopyComment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                                               // 复制
                String text = etInputComment.getText().toString();
                ClipboardManager clipboardManager = (ClipboardManager) MRAM.mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clipData = ClipData.newPlainText("Label", text);
                clipboardManager.setPrimaryClip(clipData);   // 将ClipData内容放到系统剪贴板里。
                MTOAST("已复制", 2000);
            }
        });
        findViewById(R.id.kbtSaveComment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                               // 保存评论文字
                String text = etInputComment.getText().toString();
//                String[] texts = null;
                if (text != null){
                    text = text.replace("    ", "\n");
//                    texts = text.split("\n");
                }
                switch (gLogTyp){
                    case NEW_COMMENT:
                        MAPP.mBDbsAccount.TextComment[W.TEXT_NEW] = text;
                        break;
                    case OLD_COMMENT:
                        MAPP.mBDbsAccount.TextComment[W.TEXT_OLD] = text;// OldComment = texts;
                        break;
                    case CARE_TEXT:
                        MAPP.mBDbsAccount.TextComment[W.TEXT_CARE] = text;// CareText = texts;
                        break;
                    case GIFT_TEXT:
                        MAPP.mBDbsAccount.TextComment[W.TEXT_GIFT] = text;// GiftText = texts;
                        break;
                    case ADVERT_TEXT:
                        MAPP.mBDbsAccount.TextComment[W.TEXT_ADVERT] = text;// AdvertText = texts;
                        break;
                    default:
                        MAPP.mBDbsAccount.TextComment[W.TEXT_NORMAL] = text;// NormalComment = texts;
                        break;
                }
//                MSKIN.setDbsItem(MAPP.MEMORY_ACCOUNT, 0, MAPP.mBDbsAccount.AccountName, MAPP.mBDbsAccount);
//                BRunRam bRunRam = RunUtils.getTaskEach(MAPP.mBDbsAccount);
//                if (bRunRam != null) {
//                    bRunRam.MAPP.mBDbsAccount = MAPP.mBDbsAccount;
//                }
                MTOAST(String.format("已保存%s", gsType[gLogTyp.ordinal()]));
                RunUtils.saveBDbsAccount(MAPP.mBDbsAccount);
                showInfo();
            }
        });
        etInputComment = findViewById(R.id.etInputComment);




    }

    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
        showInfo();
    }

    private void showInfo(){
        String tips;
        String text;
//        String[] texts;
        switch (gLogTyp){
            case NEW_COMMENT:
                tips = String.format("新作品表示今天发布的作品，而且还没有评论及礼物\n每条评论用换行或4个空格隔离\n#号后面跟随的字母用于变量替换\n#F表情 #W作品名 #W用户名");
                text = MAPP.mBDbsAccount.TextComment[W.TEXT_NEW]; // .NewComment;
                break;
            case OLD_COMMENT:
                tips = String.format("旧作品表示至少是昨天发布的作品，而且有评论及礼物\n每条评论用换行或4个空格隔离\n#号后面跟随的字母用于变量替换\n#F表情 #W作品名 #W用户名");
                text = MAPP.mBDbsAccount.TextComment[W.TEXT_OLD]; // OldComment;
                break;
            case CARE_TEXT:
                tips = String.format("每条评论用换行或4个空格隔离\n#号后面跟随的字母用于变量替换\n#F表情 #W作品名 #W用户名");
                text = MAPP.mBDbsAccount.TextComment[W.TEXT_CARE]; // CareText;
                break;
            case GIFT_TEXT:
                tips = String.format("每条评论用换行或4个空格隔离\n#号后面跟随的字母用于变量替换\n#F表情 #W作品名 #W用户名");
                text = MAPP.mBDbsAccount.TextComment[W.TEXT_GIFT]; // .GiftText;
                break;
            case ADVERT_TEXT:
                tips = String.format("每条评论用换行或4个空格隔离\n#号后面跟随的字母用于变量替换\n#F表情 #W作品名 #W用户名");
                text = MAPP.mBDbsAccount.TextComment[W.TEXT_ADVERT]; // .AdvertText;
                break;
            default:
                tips = String.format("每条评论用换行或4个空格隔离\n#号后面跟随的字母用于变量替换\n#F表情 #W作品名 #W用户名");
                text = MAPP.mBDbsAccount.TextComment[W.TEXT_NORMAL]; // .NormalComment;
                break;

        }
        MSKIN.setText(tips, tvTipsComment);

//        if (texts != null && texts.length > 0){
//            StringBuffer stringBuffer = new StringBuffer();
//            for (int index = 0; index < texts.length; index++){
//                stringBuffer.append(texts[index]);
//                stringBuffer.append("\n");
//            }
//            etInputComment.setText(stringBuffer.toString());
//        }
//        etInputComment.setText(MAPP.textsToText(text));
        etInputComment.setText(text);


    }

}


